package com.gcsj.gcsjspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gcsj.gcsjspringboot.common.utils.PageUtils;
import com.gcsj.gcsjspringboot.common.utils.R;
import com.gcsj.gcsjspringboot.common.validator.ValidatorUtils;
import com.gcsj.gcsjspringboot.controller.dto.SysRoleDTO;
import com.gcsj.gcsjspringboot.entity.SysMenuEntity;
import com.gcsj.gcsjspringboot.entity.SysRoleEntity;
import com.gcsj.gcsjspringboot.entity.SysUserRoleEntity;
import com.gcsj.gcsjspringboot.service.SysRoleDeptService;
import com.gcsj.gcsjspringboot.service.SysRoleMenuService;
import com.gcsj.gcsjspringboot.service.SysRoleService;
import com.gcsj.gcsjspringboot.service.SysUserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author
 */
@RestController
@RequestMapping("/sys/role")
@CrossOrigin
public class SysRoleController extends AbstractController {
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	@Autowired
	private SysUserRoleService userRoleService;

	/**
	 * 角色列表
	 */
	@RequestMapping("/list")
	public R list(@RequestBody Map<String, Object> params){
		PageUtils page = sysRoleService.queryPage(params);
		return R.ok().put("page", page);
	}

	@RequestMapping("/getRoles")
	public R getRoles(Long userId){
		List<SysRoleEntity> ids = userRoleService.queryRoleList(userId);
		return R.ok().put("data", ids);
	}

	@RequestMapping("/getAll")
	public R getAll(){
		return R.ok().put("data",sysRoleService.list());
	}

	/**
	 * 角色列表
	 */
	@RequestMapping("/select")

	public R select(){
		List<SysRoleEntity> list = sysRoleService.list();

		return R.ok().put("list", list);
	}

	/**
	 * 角色信息
	 */
	@RequestMapping("/info/{roleId}")
	public R info(@PathVariable("roleId") Long roleId){
		SysRoleEntity role = sysRoleService.getById(roleId);

		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);

//		//查询角色对应的部门
//		List<Long> deptIdList = sysRoleDeptService.queryDeptIdList(new Long[]{roleId});
//		role.setDeptIdList(deptIdList);

		return R.ok().put("role", role);
	}

	/**
	 * 保存角色
	 */
	@RequestMapping("/save")
	public R save(@RequestBody SysRoleEntity role){
		sysRoleService.saveRole(role);
		return R.ok();
	}
	@RequestMapping("/userRoleSave")
	public R userRoleSave(@RequestBody SysUserRoleEntity role){
		SysUserRoleEntity one = userRoleService.getOne(new QueryWrapper<SysUserRoleEntity>().eq("role_id", role.getRoleId())
				.eq("user_id", role.getUserId()));
		if(one == null){
			boolean save = userRoleService.save(role);
			if(save){
				return R.ok();
			}
			return R.error();
		}
		return R.error();
	}

	@RequestMapping("/userRoleDel")
	public R userRoleDel(@RequestBody SysUserRoleEntity role){
		boolean ret = userRoleService.remove(new QueryWrapper<SysUserRoleEntity>()
				.eq("user_id", role.getUserId())
				.eq("role_id", role.getRoleId()));
		if(ret){
			return R.ok();
		}
		return R.error();
	}
	@RequestMapping("/saveMenuRole")
	public R saveMenuRole(@RequestBody SysRoleDTO data){
		sysRoleMenuService.saveOrUpdate(data.getRoleId(), data.getMenuIdList());
		return R.ok();
	}
	/**
	 * 修改角色
	 */
	@RequestMapping("/update")
	public R update(@RequestBody SysRoleEntity role){
		boolean b = sysRoleService.updateById(role);
		if(b){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除角色
	 */
	@RequestMapping("/delete")
	public R delete(Integer id){
		boolean b = sysRoleService.removeById(id);
		if(b){
			return R.ok();
		}
		return R.error();
	}
}
